*** Stata 15 reproduction code for

*Mader, Matthias & Harald Schoen.
*What a difference an event can make: The European refugee crisis, party competition, and voters’ responses in Germany
*West European Politics

	
*** Figure 4: Effects of pre-crisis immigration attitude on change in party and leader approval
**************************************************************************************************

use "DATA_wide.dta", clear

*** Regressions for Figure 4

	*CDU approval	
	matrix w1w7 = J(6,3,.) // 5 rows, 3 columns 
	matrix w7w9 = J(6,3,.) // 5 rows, 3 columns 
	matrix w7w10 = J(6,3,.) // 5 rows, 3 columns 
	matrix w7w17 = J(6,3,.) // 5 rows, 3 columns 

	reg diff1_cdu7 immi1 diff1_immi7 welfare1 diff1_welfare7
	estimate store m_diff1_cdu7
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[1, 1] = r[1,1]
	matrix w1w7[1, 2] = r[5,1]
	matrix w1w7[1, 3] = r[6,1]

	reg diff7_b_cdu9 immi7 diff7_immi9 welfare7 diff7_welfare10 // for welfare distance, w7 --> w10 is used
	estimate store m_diff7_cdu9
	matrix list r(table)
	mat r=r(table)
	matrix w7w9[1, 1] = r[1,1]
	matrix w7w9[1, 2] = r[5,1]
	matrix w7w9[1, 3] = r[6,1]

	foreach i in 10 17 {
	reg diff7_b_cdu`i' immi7 diff7_immi`i' welfare7 diff7_welfare`i'
	estimate store m_diff7_cdu`i'
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[1, 1] = r[1,1]
	matrix w7w`i'[1, 2] = r[5,1]
	matrix w7w`i'[1, 3] = r[6,1]
	matrix list w7w`i'
	}

	*CSU approval	
	reg diff1_csu7 immi1 diff1_immi7 welfare1 diff1_welfare7
	estimate store m_diff1_csu7
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[2, 1] = r[1,2]
	matrix w1w7[2, 2] = r[5,2]
	matrix w1w7[2, 3] = r[6,2]

	reg diff7_b_csu9 immi7 diff7_immi9 welfare7 diff7_welfare10 // for welfare distance, w7 --> w10 is used
	estimate store m_diff7_csu9
	matrix list r(table)
	mat r=r(table)
	matrix w7w9[2, 1] = r[1,1]
	matrix w7w9[2, 2] = r[5,1]
	matrix w7w9[2, 3] = r[6,1]

	foreach i in 10 17 {
	reg diff7_b_csu`i' immi7 diff7_immi`i' welfare7 diff7_welfare`i'
	estimate store m_diff7_csu`i'
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[2, 1] = r[1,1]
	matrix w7w`i'[2, 2] = r[5,1]
	matrix w7w`i'[2, 3] = r[6,1]
	matrix list w7w`i'
	}

	*AfD approval
	reg diff1_afd7 immi1 diff1_immi7 welfare1 diff1_welfare7
	estimate store m_diff1_afd7
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[3, 1] = r[1,1]
	matrix w1w7[3, 2] = r[5,1]
	matrix w1w7[3, 3] = r[6,1]

 	reg diff7_b_afd9 immi7 diff7_immi9 welfare7 diff7_welfare10 // for welfare distance, w7 --> w10 is used
	estimate store m_diff7_afd9
	matrix list r(table)
	mat r=r(table)
	matrix w7w9[3, 1] = r[1,1]
	matrix w7w9[3, 2] = r[5,1]
	matrix w7w9[3, 3] = r[6,1]

	foreach i in 10 17 {
	reg diff7_b_afd`i' immi7 diff7_immi`i' welfare7 diff7_welfare`i'
	estimate store m_diff7_afd`i'
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[3, 1] = r[1,1]
	matrix w7w`i'[3, 2] = r[5,1]
	matrix w7w`i'[3, 3] = r[6,1]
	matrix list w7w`i'
	}

	*Merkel approval
	reg diff1_merkel7 immi1 diff1_immi7 welfare1 diff1_welfare7
	estimate store m_diff1_merkel7
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[4, 1] = r[1,1]
	matrix w1w7[4, 2] = r[5,1]
	matrix w1w7[4, 3] = r[6,1]

 	reg diff7_merkel9 immi7 diff7_immi9 welfare7 diff7_welfare10 // for welfare distance, w7 --> w10 is used
	estimate store m_diff7_merkel9
	matrix list r(table)
	mat r=r(table)
	matrix w7w9[4, 1] = r[1,1]
	matrix w7w9[4, 2] = r[5,1]
	matrix w7w9[4, 3] = r[6,1]
	
	foreach i in 10 17 {
	reg diff7_merkel`i' immi7 diff7_immi`i' welfare7 diff7_welfare`i'
	estimate store m_diff7_merkel`i'
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[4, 1] = r[1,1]
	matrix w7w`i'[4, 2] = r[5,1]
	matrix w7w`i'[4, 3] = r[6,1]
	matrix list w7w`i'
	}

	*Seehofer approval
	reg diff1_seeh7 immi1 diff1_immi7 welfare1 diff1_welfare7
	estimate store m_diff1_seeh7
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[5, 1] = r[1,1]
	matrix w1w7[5, 2] = r[5,1]
	matrix w1w7[5, 3] = r[6,1]
	
 	reg diff7_seeh9 immi7 diff7_immi9 welfare7 diff7_welfare10 // for welfare distance, w7 --> w10 is used
	estimate store m_diff7_seeh9
	matrix list r(table)
	mat r=r(table)
	matrix w7w9[5, 1] = r[1,1]
	matrix w7w9[5, 2] = r[5,1]
	matrix w7w9[5, 3] = r[6,1]

	foreach i in 10 17 {
	reg diff7_seeh`i' immi7 diff7_immi`i' welfare7 diff7_welfare`i'
	estimate store m_diff7_seeh`i'
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[5, 1] = r[1,1]
	matrix w7w`i'[5, 2] = r[5,1]
	matrix w7w`i'[5, 3] = r[6,1]
	matrix list w7w`i'
	}

	*Petry approval
	reg diff2_petry6 immi2 diff2_immi6 welfare2 diff2_welfare6
	estimate store m_diff2_petry6
	matrix list r(table)
	mat r=r(table)
	matrix w1w7[6, 1] = r[1,1] // this is, of course, not really a w1w7 result, but treated as such
	matrix w1w7[6, 2] = r[5,1]
	matrix w1w7[6, 3] = r[6,1]
	
 	reg diff6_petry9 immi6 diff6_immi9 welfare6 diff6_welfare10 // for welfare distance, w6 --> w10 is used
	estimate store m_diff6_petry9
	matrix list r(table)
	mat r=r(table)
	matrix w7w9[6, 1] = r[1,1] // this is, of course, not really a w7w9 result, but treated as such
	matrix w7w9[6, 2] = r[5,1]
	matrix w7w9[6, 3] = r[6,1]

	foreach i in 10 17 {
	reg diff6_petry`i' immi6 diff6_immi`i' welfare6 diff6_welfare`i'
	estimate store m_diff6_petry`i'
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[6, 1] = r[1,1] // this is, of course, not really a w7w`i' result, but treated as such
	matrix w7w`i'[6, 2] = r[5,1]
	matrix w7w`i'[6, 3] = r[6,1]
	matrix list w7w`i'
	}

*** Create Figure 4
	grstyle init
	grstyle set imesh, horizontal 
	coefplot (matrix(w1w7[,1]), ci((w1w7[,2] w1w7[,3])) msymbol(circle) mcolor(gs0) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.25)) ///
			  (matrix(w7w9[,1]), ci((w7w9[,2] w7w9[,3])) msymbol(circle) mcolor(gs6) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(0.08)) ///
			 (matrix(w7w10[,1]), ci((w7w10[,2] w7w10[,3])) msymbol(circle) mcolor(gs13) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-.08)) ///
			 (matrix(w7w17[,1]), ci((w7w17[,2] w7w17[,3])) msymbol(circle) mcolor(gs16) mlcolor(gs0) msize(vlarge) ciopts(recast(rbar) barwidth(0.03) color(gs0)) offset(-0.25)) ///
			, /// 
			coeflabels(r1="{&Delta} CDU approval" r2="{&Delta} CSU approval" r3="{&Delta} AfD approval" r4="{&Delta} Merkel approval" r5="{&Delta} Seehofer approval" r6="{&Delta} Petry approval" ,labsize(large)  labcolor(gs0)) ///
			xscale(range(-.6 .6)) xlabel(-.6 (.2) .6, noticks labsize(large) labcolor(gs0)) xmtick(none) ///
			legend(order(2 "w1-w4" 4 "w4-w5" 6 "w4-w6" 8 "w4-w10") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-5 0 -5 0)) ysize(2.5) xsize(3.5)  /// scale(.8)
			title("Effects of prior immigration attitude on...", size(large)  color(gs0)) ///
			name(fig4, replace)
	
	
*** Appendix: Regression tables for Figure 4
***********************************************

	foreach var in cdu csu afd {
		esttab m_diff1_`var'7 m_diff7_`var'9 m_diff7_`var'10 m_diff7_`var'17 using Tables_Figure4_`var'.rtf, replace /// 
					stats(r2 N , fmt(%9.2g) labels("R2" "N")) ///
					cells(b(star fmt(2)) se(par fmt(2) )) ///
					mtitles("w1->w7" "w7->w9" "w7->w10" "w7->w17") ///
					varlabels ///
						(immi1 "Immigration attitude (w1)" ///
						 diff1_immi7 "Change in immigration attitude (w1-w4)" ///
						 immi7  "Immigration attitude (w4)" ///
						 diff7_immi9  "Change in immigration attitude (w4-w5)" ///
						 diff7_immi10  "Change in immigration attitude (w4-w6)" ///
						 diff7_immi17  "Change in immigration attitude (w4-w10)" ///
						 welfare1 "Welfare attitude (w1)" ///
						 diff1_welfare7 "Change in welfare attitude (w1-w4)" ///
						 welfare7  "Welfare attitude (w4)" ///
						 diff7_welfare10  "Change in welfare attitude (w4-w6)" ///
						 diff7_welfare17  "Change in welfare attitude (w4-w10)" ///
						 _cons "Constant") ///
					title("Table: Regression results for xxx approval") ///
					addnotes("Notes: Reported are OLS coefficients with standard errors in parentheses. *: p<0,05, **: p<0,01, ***: p<0,001.") ///
					varwidth(40) modelwidth(15)	
	}
	foreach var in merkel seeh {
		esttab m_diff1_`var'7 m_diff7_`var'9 m_diff7_`var'10 m_diff7_`var'17 using Tables_Figure4_`var'.rtf, replace /// 
					stats(r2 N , fmt(%9.2g) labels("R2" "N")) ///
					cells(b(star fmt(2)) se(par fmt(2) )) ///
					mtitles("w1->w7" "w1->w9" "w1->w10" "w7->w17") ///
					varlabels ///
						(immi1 "Immigration attitude (w1)" ///
						 diff1_immi7 "Change in immigration attitude (w1-w4)" ///
						 immi7  "Immigration attitude (w4)" ///
						 diff7_immi9  "Change in immigration attitude (w4-w5)" ///
						 diff7_immi10  "Change in immigration attitude (w4-w6)" ///
						 diff7_immi17  "Change in immigration attitude (w4-w10)" ///
						 welfare1 "Welfare attitude (w1)" ///
						 diff1_welfare7 "Change in welfare attitude (w1-w4)" ///
						 welfare7  "Welfare attitude (w4)" ///
						 diff7_welfare10  "Change in welfare attitude (w4-w6)" ///
						 diff7_welfare17  "Change in welfare attitude (w4-w10)" ///
						 _cons "Constant") ///
					title("Table: Regression results for xxx approval") ///
					addnotes("Notes: Reported are OLS coefficients with standard errors in parentheses. *: p<0,05, **: p<0,01, ***: p<0,001.") /// 
					varwidth(40) modelwidth(15)	
	}	
		esttab m_diff2_petry6 m_diff6_petry9 m_diff6_petry10 m_diff6_petry17 using Tables_Figure4_petry.rtf, replace /// 
					stats(r2 N , fmt(%9.2g) labels("R2" "N")) ///
					cells(b(star fmt(2)) se(par fmt(2) )) ///
					mtitles("w2->w3" "w3->w5" "w3->w6" "w3->w10") ///
					varlabels ///
						(immi2 "Immigration attitude (w2)" ///
						 diff2_immi6 "Change in immigration attitude (w2-w3)" ///
						 immi6  "Immigration attitude (w3)" ///
						 diff6_immi9  "Change in immigration attitude (w3-w5)" ///
						 diff6_immi10  "Change in immigration attitude (w3-w6)" ///
						 diff6_immi17  "Change in immigration attitude (w3w10)" ///
						 welfare2 "Welfare attitude (w2)" ///
						 diff2_welfare6 "Change in welfare attitude (w2-w3)" ///
						 welfare6  "Welfare attitude (w3)" ///
						 diff6_welfare10  "Change in welfare attitude (w3-w6)" ///
						 diff6_welfare17  "Change in welfare attitude (w3-w10)" ///
						 _cons "Constant") ///
					title("Table: Regression results for Petry approval") ///
					addnotes("Notes: Reported are OLS coefficients with standard errors in parentheses. *: p<0,05, **: p<0,01, ***: p<0,001.") /// 
					varwidth(40) modelwidth(15)	
